package org.jeecg.modules.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.modules.generator.entity.SysGenerateFieldInfoEntity;
import org.jeecg.modules.generator.vo.SysGenerateFieldInfoVo;
import org.jeecg.modules.generator.service.SysGenerateFieldInfoService;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Description 生成字段及其他属性配置表控制器
 * @Author Administrator
 * @Date 2023-05-06 10:32:17
 */
@RestController
@RequestMapping(value = "generator/TbGenerateFieldInfo")
public class SysGenerateFieldInfoController {

    @Autowired
    private SysGenerateFieldInfoService sysGenerateFieldInfoService;


    /**
     * 查询生成字段及其他属性配置表分页数据
     *
     * @param sysGenerateFieldInfoVo
     * @return
     */
    @GetMapping(value = "list")
    public Result<IPage<SysGenerateFieldInfoEntity>> list(SysGenerateFieldInfoVo sysGenerateFieldInfoVo) {

        Result<IPage<SysGenerateFieldInfoEntity>> result = new Result<>();
        QueryWrapper queryWrapper = sysGenerateFieldInfoVo.getQueryWrapper();
        IPage<SysGenerateFieldInfoEntity> pageList = sysGenerateFieldInfoService.page(sysGenerateFieldInfoVo.getPage(), queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 根据主键查询生成字段及其他属性配置表对象数据
     *
     * @param id
     * @return
     */
    @GetMapping(value = "queryById/{id}")
    public Result queryById(@PathVariable("id") String id) {

        SysGenerateFieldInfoEntity sysGenerateFieldInfoEntity = sysGenerateFieldInfoService.getById(id);
        if (sysGenerateFieldInfoEntity != null) {
            return Result.ok(sysGenerateFieldInfoEntity);
        } else {
            return Result.error("未找到查询对象！");
        }
    }

    /**
     * 生成字段及其他属性配置表新增保存
     *
     * @param sysGenerateFieldInfoEntity
     * @return
     */
    @PostMapping(value = "add")
    public Result add(@RequestBody SysGenerateFieldInfoEntity sysGenerateFieldInfoEntity) {
        sysGenerateFieldInfoService.save(sysGenerateFieldInfoEntity);
        return Result.ok("新增生成字段及其他属性配置表成功！");
    }

    /**
     * 生成字段及其他属性配置表修改保存
     *
     * @param sysGenerateFieldInfoEntity
     * @return
     */
    @PutMapping(value = "edit")
    public Result edit(@RequestBody SysGenerateFieldInfoEntity sysGenerateFieldInfoEntity) {

        SysGenerateFieldInfoEntity generateFieldInfoEntity = sysGenerateFieldInfoService.getById(sysGenerateFieldInfoEntity.getId());
        if (generateFieldInfoEntity != null) {
            sysGenerateFieldInfoService.updateById(sysGenerateFieldInfoEntity);
            return Result.ok("修改生成字段及其他属性配置表成功！");
        } else {
            return Result.error("修改生成字段及其他属性配置表不存在！");
        }
    }

    /**
     * 生成字段及其他属性配置表删除
     *
     * @param id
     * @return
     */

    @DeleteMapping(value = "delete")
    public Result delete(@RequestParam("id") String id) {

        sysGenerateFieldInfoService.removeById(id);
        return Result.ok("删除生成字段及其他属性配置表数据成功！");
    }
}
